<?php
/**
 * Created by PhpStorm.
* User: wudiying
* Date: 2020/2/17
* Time: 上午1:06
*/


/**
 * swoole之WebSocket服务器

    new swoole_websocket_server();
    swoole_websocket_server 继承自swoole_http_server;
    on/start函数；
    open/message/close；//on函数的事件参数
    push()发送数据。

 */
$host = '0.0.0.0';
$port = 8034;

$webSocket = new swoole_websocket_server($host,$port);


// on

// open 建立连接 $webSocket:服务器,$request：客户端信息
$webSocket->on('open',function ($webSocket,$request){
    var_dump($webSocket);
    var_dump($request);
    $webSocket->push($request->fd,"  open\n");
});


// message 接收信息
$webSocket->on('message',function ($webSocket,$request){
    echo "message: $request->data";
    $webSocket->push($request->fd,"  message\n");
});

// close 关闭连接

$webSocket->on('close',function ($webSocket,$request){
    echo "close:\n";
});

$webSocket->start();
